package cn.xlmdz.subsoilerinfoquery.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kevin.delegationadapter.extras.ClickableAdapterDelegate;

import org.jetbrains.annotations.NotNull;

import cn.xlmdz.subsoilerinfoquery.bean.SubsoilDataListBean;
import cn.xlmdz.subsoilerinfoquery.databinding.QuerySingleListItemBinding;
import cn.xlmdz.subsoilerinfoquery.ui.MainActivity;
import cn.xlmdz.subsoilerinfoquery.ui.MapActivity;
import cn.xlmdz.subsoilerinfoquery.ui.QuerySingleActivity;
import cn.xlmdz.subsoilerinfoquery.utils.ToastUtil;

public class QuerySingleListAdapter extends ClickableAdapterDelegate<SubsoilDataListBean.Work, QuerySingleListAdapter.ShowContentAdapterVH> {
    private Context mContext;
    private int mItemType;
    private int mDataType;
    private SubsoilDataListBean.Info info;

    public QuerySingleListAdapter(Context Context, int itemType) {
        this.mContext = Context;
        this.mItemType = itemType;
    }

    @NotNull
    @Override
    public ShowContentAdapterVH onCreateViewHolder(@NotNull ViewGroup viewGroup) {
        QuerySingleListItemBinding listItemBinding = QuerySingleListItemBinding.inflate(LayoutInflater.from(viewGroup.getContext()), viewGroup, false);
        return new ShowContentAdapterVH(listItemBinding);
    }

    @Override
    public void onItemClick(@NotNull View view, SubsoilDataListBean.Work item, int position) {
        super.onItemClick(view, item, position);
        //ToastUtil.showTextToast(mContext, "点击了第" + ++position + "个条目");

        if (info != null) {
            Intent intent = new Intent(mContext, MapActivity.class);
            intent.putExtra("cid", info.getId());
            intent.putExtra("date", item.getDate());
            intent.putExtra("position", item.getPosition());
            intent.putExtra("terminal", info.getTerminal().getTerminal_id());
            intent.putExtra("area", item.getManual_area());
            mContext.startActivity(intent);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ShowContentAdapterVH holder, int position, SubsoilDataListBean.Work item) {
        super.onBindViewHolder(holder, position, item);

        // 设置作业日期
        String date = item.getDate();
        holder.listItemBinding.tvWorkDate.setText(date);

        // 设置面积
//        String area = "0.0";
//        if (mDataType == 1) {
//            area = item.getAuto_area();
//        }else {
//            area = item.getManual_area();
//        }
        holder.listItemBinding.tvArea.setText(item.getManual_area().toString());

        // 设置作业日期
        String location = item.getPosition();
        holder.listItemBinding.tvPosition.setText(location);
    }

    @Override
    public int getItemViewType() {
        return mItemType;
    }

    public static class ShowContentAdapterVH extends BaseViewHolder {
        QuerySingleListItemBinding listItemBinding;

        public ShowContentAdapterVH(QuerySingleListItemBinding listItemBinding) {
            super(listItemBinding.getRoot());

            this.listItemBinding = listItemBinding;
        }
    }

    public void setInfo(SubsoilDataListBean.Info info) {
        this.info = info;
    }

    /**
     * 设置要显示的数据类型
     *
     * @param type 1，自动。2，手动
     */
//    public void setDataType(int type) {
//        mDataType = type;
//    }
}
